package com.creative.accountsdk.holder;

import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.creative.accountsdk.AccountSdk;
import com.creative.accountsdk.DataCenter;
import com.creative.accountsdk.DialogUtils;
import com.creative.accountsdk.OnLoginListener;
import com.creative.accountsdk.R;
import com.creative.accountsdk.net.XHttpHandler;
import com.creative.accountsdk.net.account.TeacherInviteProtocol;

import org.json.JSONObject;

import cz.msebera.android.httpclient.Header;

/**
 * 选择身份
 * Created by David on 2015/11/25.
 */
public class SelectIdentityHolder extends BaseHolder {

    ImageView ivPatriarch,ivChild,ivTeacher;
    Button    btnNext;
    @Override
    protected void initViews() {
        ivPatriarch = (ImageView)viewRoot.findViewById(R.id.iv_ident_patriarch);
        ivChild = (ImageView)viewRoot.findViewById(R.id.iv_ident_child);
        ivTeacher = (ImageView)viewRoot.findViewById(R.id.iv_ident_teacher);
        btnNext = (Button)viewRoot.findViewById(R.id.btn_ident_next);
        setOnListener();
    }

    @Override
    public int getLayoutResID() {
        return R.layout.account_select_identity;
    }
    int  currentRole = 0;
    private void  setOnListener(){
         ivPatriarch.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 currentRole = Constants.PATRIARCH;
                 isCheck(v);
             }
         });
         ivChild.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 currentRole = Constants.CHILD;
                 isCheck(v);
             }
         });
        ivTeacher.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                currentRole = Constants.TEACHER;
                isCheck(v);
            }
        });

        btnNext.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (currentRole != Constants.TEACHER) {
                    setRole();
                } else {
                    toTeacherInvite();
                }
            }
        });
    }

    private void  isCheck(View v){
        ivPatriarch.setImageResource(R.drawable.btn_circle_normal);
        ivChild.setImageResource(R.drawable.btn_circle_normal);
        ivTeacher.setImageResource(R.drawable.btn_circle_normal);

        ((ImageView)v).setImageResource(R.drawable.btn_circle_down);
    }

    private void  toTeacherInvite(){
        activity.hideHolder();
        activity.showHolder(new TeacherInviteCodeHolder(currentRole));
    }

    private void setRole() {
        progress = DialogUtils.createProgress(activity);
        progress.show();
        final TeacherInviteProtocol protocol = new TeacherInviteProtocol(currentRole, null, null);


        protocol.debugUrl(DataCenter.getHttpClient());
        DataCenter.getHttpClient().post(protocol.getUrl(), protocol.getParams(), new XHttpHandler(activity, progress) {
            @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response) {
                super.onSuccess(statusCode, headers, response);
                progress.cancel();
                if (protocol.parse(response, activity)) {
                    OnLoginListener loginListener = AccountSdk.getInstance().getLoginListener();
                    if (loginListener != null) {
                        loginListener.onComplete(protocol.jsonUser);
                    }
                }
                toUserInfor();
            }
        });
    }

    private void  toUserInfor(){
        activity.hideHolder();
        activity.userInfoHolder =new UserInfoHolder(activity);
        activity.showHolder(activity.userInfoHolder);
    }

}
